package com.zhuge_new;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.StaticLayout;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.lzy.okgo.utils.OkLogger;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.header.waveswipe.DropBounceInterpolator;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshInitializer;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * Created by lenovo on 2018-08-24.
 */

public class App extends Application{


  public static Gson gson;

  @Override
  public void onCreate() {
    super.onCreate();
    gson = new GsonBuilder().setPrettyPrinting().create();
      ZXingLibrary.initDisplayOpinion(this);
    OkHttpClient.Builder builder= new OkHttpClient.Builder();

    //初始化配置
      OkGo.getInstance().init(this)
              .setOkHttpClient(builder.build())
              .setCacheMode(CacheMode.NO_CACHE)
              .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
              .setRetryCount(3);
      //log相关
    HttpLoggingInterceptor loggingInterceptor= new HttpLoggingInterceptor("OkGo");
    loggingInterceptor.setColorLevel(Level.INFO);
    loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
    builder.addInterceptor(loggingInterceptor);

      builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);    //全局的读取超时时间
      builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);   //全局的写入超时时间
      builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS); //全局的连接超时时间

      //http相关
      HttpsUtils.SSLParams sslParamsl=HttpsUtils.getSslSocketFactory();
      builder.sslSocketFactory(sslParamsl.sSLSocketFactory,sslParamsl.trustManager);

      refresh();
  }

    private void refresh() {
        SmartRefreshLayout.setDefaultRefreshInitializer(new DefaultRefreshInitializer() {
            @Override
            public void initialize(@NonNull Context context, @NonNull RefreshLayout layout) {
                layout.setReboundDuration(1000);
                layout.setReboundInterpolator(new DropBounceInterpolator());
                layout.setFooterHeight(100);
                layout.setDisableContentWhenLoading(false);
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
            }
        });

        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //开始设置全局的基本参数（这里设置的属性只跟下面的MaterialHeader绑定，其他Header不会生效，能覆盖DefaultRefreshInitializer的属性和Xml设置的属性）
                layout.setEnableHeaderTranslationContent(false);
                return new MaterialHeader(context).setColorSchemeResources(R.color.red,R.color.white,R.color.black);
            }
        });
    }

}
